package lgq.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import util.DBUtil;
import util.Page;
import dao.EmpDAO;
import dao.impl.EmpDAOImpl;
import model.Emp;

public class EmpManager {
	public Emp getById(int id)
	{
		EmpDAO ed=new EmpDAOImpl();
		return ed.getEmpById(id);
	}
	public int getIdByEmpNo(String empno)
	{
		EmpDAO ed=new EmpDAOImpl();
		return ed.getIDByEmpno(empno);
	}
	public Emp getByEmpNo(String empno)
	{
		return this.getById(this.getIdByEmpNo(empno));
	}
	
	public boolean addEmpPhoto(int empid,String photo)
	{
		DBUtil dbu = new DBUtil();
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		boolean flag=false;
		try {
			conn=dbu.getConnection();
			String sql="update emp set photo=?  where id=?";
			pstat=conn.prepareStatement(sql);
			pstat.setString(1, photo);
			pstat.setInt(2, empid);
			pstat.executeUpdate();
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return flag;
	}
	
	public boolean addEmpEducationImg(int empid,String educationImg)
	{
		DBUtil dbu = new DBUtil();
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		boolean flag=false;
		try {
			conn=dbu.getConnection();
			String sql="update emp set educationimg=?  where id=?";
			pstat=conn.prepareStatement(sql);
			pstat.setString(1, educationImg);
			pstat.setInt(2, empid);
			pstat.executeUpdate();
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return flag;
	}
	
	public Page<Emp>getAll(int pageNo, int pageSize)
	{
		EmpDAO ed=new EmpDAOImpl();
		return ed.getAllEmp(pageNo, pageSize);
	}
	
	public Page<Emp> getByCode(int pageNo,int pageSize,String name,String sex,String edu,String skill)
	{
		return new EmpDAOImpl().getByCode(pageNo, pageSize, name, sex, edu, skill);
	}
	public boolean updateEmp(Emp e) {
		return new EmpDAOImpl().updateEmp(e);
	}
}
